// pages/user_end/address_add/address_add.js
const app = getApp();
const request = require('../../../utils/request.js');
const url = require('../../../utils/url.js');
const cookie = require('../../../utils/cookie.js');
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id:0,
    token:'',
    region: [],
    index: 0,
    name:'',
    phone:'',
    addr:'',
    addrdetail:'',
    address:'',
    postcode:'',
    setlocation:0,
    lat:0,
    lng:0,
    is_default:1
  },
  bindPickerChange(e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    console.log(e.detail);
    this.setData({
      region : e.detail.value,
      postcode : e.detail.postcode
    })
   
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(typeof(options.id)=='string'){
      this.setData({
        id:options.id
      })
      wx.setNavigationBarTitle({
        title:'编辑地址'
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    var that = this;
    cookie.getCookie('userInfo', function (res) {
      if (res) {
        var etime = Date.parse(new Date()) / 1000;
        
        if (etime < res.expiretime) {
          that.setData({
            login: 1,
            token:res.token
          })
          if(that.data.id>0){
            that.getAddress();
          }
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  choselocation(){
     var that = this;
     wx.chooseLocation({
       success: function(res) {
         console.log("地图点击事件：" + JSON.stringify(res));
         that.setData({
           addr:res.address,
           lat : res.latitude,
           lng: res.longitude,
           setlocation:1 
         });

       }
     })
  },
  changeDefault:function(){
    this.setData({
      is_default:!this.data.is_default
    })
  },
  changename:function(e){
     this.setData({
       name:e.detail.value
     })
  },
 changephone:function(e){
    this.setData({
      phone:e.detail.value
    })
 },
 changeaddress:function(e){
     this.setData({
        addrdetail:e.detail.value
     })
 },
 getAddress:function(){
  var that = this;
  let param = {
    scoure: 'wxapp',
    id:that.data.id,
    token:that.data.token
  }
  wx.showNavigationBarLoading(); //在当前页面显示导航条加载动画
  request.request(url.addressdetail, param, url.REQUEST_METHOD_GET).then((res) => {
    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
    if(res.data.code==1){
      that.setData({
         name:res.data.data.name,
         phone:res.data.data.phone,
         addr:res.data.data.addr,
         addrdetail:res.data.data.addr_detail,
         is_default:res.data.data.is_default,
         lat:res.data.data.lat,
         lng:res.data.data.lng,
         setlocation:1
      })
     }
  })  	
},
 editAddress:function(e){
  var that = this;
  if(!that.data.name){
    wx.showToast({title: '请输入收货人!',icon:'none',duration:2000})
    return false;
  }
  if(!that.data.phone){
    wx.showToast({title: '请输入手机号码!',icon:'none',duration:2000})
    return false;
  }
  var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (!myreg.test(that.data.phone)) {
    wx.showToast({title: '请输入正确的手机号码',icon: 'none', duration: 2000}) ;     
      return false;
  }

  if(!that.data.addr){
    wx.showToast({title: '请选择服务地址!',icon:'none',duration:2000})
    return false;
  }

  if(!that.data.addrdetail){
    wx.showToast({title: '请填写详细地址!',icon:'none',duration:2000})
    return false;
  }

  let param = {
    id:that.data.id,
    name:that.data.name,
    phone:that.data.phone,
    addr:that.data.addr,
    addr_detail:that.data.addrdetail,
    lat:that.data.lat,
    lng:that.data.lng,
    is_default:that.data.is_default,
    token:that.data.token
  }
  wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
  wx.showLoading({  //显示loding提示框
    title: '正在加载数据中...',
  })
  request.request(url.editaddr, param, url.REQUEST_METHOD_POST).then((res) => {
    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
    wx.hideLoading();  //隐藏loading提示框
    wx.showToast({ title: res.data.msg,icon:'none',duration:2000});
      if(res.data.code==1){
        setTimeout(function(){
         wx.navigateBack({
           delta: 1,
         })
        },2000)
      }
 })
},
addAddress:function(e){
  var that = this;
  if(!that.data.name){
    wx.showToast({title: '请输入收货人!',icon:'none',duration:2000})
    return false;
  }
  if(!that.data.phone){
    wx.showToast({title: '请输入手机号码!',icon:'none',duration:2000})
    return false;
  }
  var myreg=/^[1][3,4,5,6,7,8,9][0-9]{9}$/;
  if (!myreg.test(that.data.phone)) {
    wx.showToast({title: '请输入正确的手机号码',icon: 'none', duration: 2000}) ;     
      return false;
  }
 
  if(!that.data.addr){
    wx.showToast({title: '请选择服务地址!',icon:'none',duration:2000})
    return false;
  }

  if(!that.data.addrdetail){
    wx.showToast({title: '请填写详细地址!',icon:'none',duration:2000})
    return false;
  }

  let param = {
    name:that.data.name,
    phone:that.data.phone,
    addr:that.data.addr,
    addr_detail:that.data.addrdetail,
    lat:that.data.lat,
    lng:that.data.lng,
    is_default:that.data.is_default,
    token:that.data.token
  }
  wx.showNavigationBarLoading();  //在当前页面显示导航条加载动画
  wx.showLoading({  //显示loding提示框
    title: '正在加载数据中...',
  })
  request.request(url.editaddr, param, url.REQUEST_METHOD_POST).then((res) => {
    wx.hideNavigationBarLoading(); //在当前页面隐藏导航条加载动画
    wx.hideLoading();  //隐藏loading提示框
    wx.showToast({ title: res.data.msg,icon:'none',duration:2000});
      if(res.data.code==1){
        setTimeout(function(){
         wx.navigateBack({
           delta: 1,
         })
        },2000)
      }
 })
},
})